#include "downfall.h"
#include "ui_downfall.h"
#include<QPainter>
#include"downfallenemy.h"
#include"downmap.h"
#include"downbullet.h"
#include<QIcon>
#include<QTimer>
#include<QPainter>
#include<QMouseEvent>
#include<QKeyEvent>
#include<QFont>
#include<QMessageBox>
#include"downbullet.h"



DownFall::DownFall(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::DownFall)
{

    //调用初始化场景
    initScene();
    //启动游戏
    PlayGame();
    ui->setupUi(this);
}

DownFall::~DownFall()
{
    delete ui;
}


void DownFall::initScene(){
    //设置窗口固定尺寸
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //设置标题
    setWindowTitle(GAME_TITLE);
    //设置图标
    setWindowIcon(QIcon(GAME_ICON));
    //设置定时器
    Timer.setInterval(GAME_RATE);

    //敌机出场间隔初始化
    recorder=0;



    //随机数种子
    srand( (unsigned int) time (NULL) );
}


void DownFall::PlayGame(){
    //启动定时器
    Timer.start();
    //监听定时器发送的信号
    connect(&Timer,&QTimer::timeout,[=](){

        //敌机出场
        EnemyToScene();

        //更新游戏坐标
        UpdatePosition();
        //绘制于屏幕中
        update();
        //碰撞检测调用
        collisionDetection();
    });

};

int once=1;//用于防止更新位置时重复报Lose
void DownFall::UpdatePosition(){
    //更新地图坐标
    map.MapPos();
    //发射子弹
    Player.shoot();

    //计算所有非空闲子弹当前坐标
    //飞机子弹
    for(int i=0;i<BULLET_NUM;i++){
        //如果非空闲，计算发射位置
        if(Player.p_bullets[i].b_Free==false){
            Player.p_bullets[i].updatePosition();
        }
        if(Player.p_bullets2[i].b_Free==false){
            Player.p_bullets2[i].updatePosition();
        }



    }

    //敌机出场
    for(int i=0;i<ENEMY_NUM;i++){
        if(enemys[i].Free==false){
            enemys[i].updatePosition();
        }
    }
    //敌机被撞
    for(int i=0;i<BOMB_NUM;i++){
        if(bombs[i].Free==false){
            bombs[i].updateInfo();
        }
    }




    //飞机爆炸
    if(PlayerBomb.Free==false&&once==1){
        PlayerBomb.updateInfo();
        this->close();
        once=0;
        QMessageBox::information(nullptr,"GAMEOVER","YOU LOSE!");
    }


};
void DownFall::paintEvent(QPaintEvent *){
    QPainter painter(this);
    //绘制地图
    painter.drawPixmap(0,map.map1_posy,map.map1);
    painter.drawPixmap(0,map.map2_posy,map.map2);
    //绘制飞机
    painter.drawPixmap(Player.p_X,Player.p_Y,Player.DownPlayer);
    //绘制子弹(非空闲)(飞机)
    for(int i=0,id=1;i<BULLET_NUM;i++){

        if(Player.p_bullets[i].b_Free==false){

            if(id==9)id=1;
            if(id<=8){
            QString path("://rsc/DownFall/bullet0");
            path+=QString::number(id);
            path+=".png";
            Player.p_bullets[i].b_Bullet.load(path);
            id++;
            }
            painter.drawPixmap(Player.p_bullets[i].b_X,Player.p_bullets[i].b_Y,Player.p_bullets[i].b_Bullet);

        }
    }

    //绘制敌机
    for(int i=0;i<ENEMY_NUM;i++){
        if(enemys[i].Free==false){
            painter.drawPixmap(enemys[i].X,enemys[i].Y,enemys[i].enemy);
        }
    }


    //绘制爆炸
    for(int i=0;i<BOMB_NUM;i++){
        if(bombs[i].Free==false){
            painter.drawPixmap(bombs[i].bombX,bombs[i].bombY,bombs[i].BombPixArr[bombs[i].bombindex]);
            SCORE+=0.1;
        }
    }

    if(PlayerBomb.Free==false){
        painter.drawPixmap(PlayerBomb.bombX,PlayerBomb.bombY,PlayerBomb.PlayerPixArr[PlayerBomb.bombindex]);
    }


    for(int i=0;i<BOMB_NUM;i++){
        if(bombs[i].Free==false){
            painter.drawPixmap(bombs[i].bombX,bombs[i].bombY,bombs[i].BombPixArr[bombs[i].bombindex]);
            SCORE+=0.1;
        }
    }

    if(PlayerBomb.Free==false){
        painter.drawPixmap(PlayerBomb.bombX,PlayerBomb.bombY,PlayerBomb.PlayerPixArr[PlayerBomb.bombindex]);
    }
    //绘制分数
    painter.setPen(Qt::darkCyan);
    painter.drawText(50,50,"分 数:");
    painter.drawText(100,50,QString::number(SCORE));
    painter.setPen(Qt::red);
    painter.drawText(50,100,"血 量");
    painter.drawText(100,100,QString::number(Player.Blood));
}
void DownFall::mouseMoveEvent(QMouseEvent * event){
    int x=event->x()-Player.DownPlayer.width()/2;
    int y=event->y()-Player.DownPlayer.height()/2;
    if(x<=0){
        x=0;
    }
    if(x>=GAME_WIDTH-Player.DownPlayer.width()){
        x=GAME_WIDTH-Player.DownPlayer.width();
    }
    if(y>=GAME_HEIGHT-Player.DownPlayer.height()){
        y=GAME_HEIGHT-Player.DownPlayer.height();
    }
    if(y<=0){
        y=0;
    }
    Player.setPosition(x,y);


}

void DownFall::keyPressEvent(QKeyEvent *event)
{


    if(event->key()==Qt::Key_Space){
        //int x=event->Player.X
        //int y=event->Player.Y;

    }
}

void DownFall::EnemyToScene(){
    recorder++;
    if(recorder<ENEMY_INTERVAL){//未到达出场间隔
        return;
    }
    for(int i=0;i<ENEMY_NUM;i++){
        //若为空闲敌机则出场
        if(enemys[i].Free){
            enemys[i].Free=false;
            enemys[i].X=rand()%(GAME_WIDTH-enemys[i].Rect.width());
            enemys[i].Y=-enemys[i].Rect.height();
            break;
        }
    }
}


void DownFall::collisionDetection(){
    //遍历所有非空闲敌机
    for(int i=0;i<ENEMY_NUM;i++){
        //空闲飞机进行下一次循环
        if(enemys[i].Free){
            continue;
        }
        //非空闲飞机，再遍历非空闲子弹
        for(int j=0;j<BULLET_NUM;j++){
            //空闲子弹进行下一次循环
            if(Player.p_bullets[j].b_Free){
                continue;
            }
            //非空闲子弹
            //子弹飞机相交，发生碰撞,将碰前状态设为真
            if(enemys[i].Rect.intersects(Player.p_bullets[j].b_Rect)){
                enemys[i].Free=true;
                Player.p_bullets[j].b_Free=true;
                //播放爆炸效果
                for(int k=0;k<BOMB_NUM;k++){
                    if(bombs[k].Free){
                        //空闲爆炸
                        bombs[k].Free=false;
                        //更新爆炸坐标
                        bombs[k].bombX=enemys[i].X;
                        bombs[k].bombY=enemys[i].Y;
                        break;
                    }
                }
            }
            if(enemys[i].Rect.intersects(Player.p_bullets2[j].b_Rect)){
                enemys[i].Free=true;
                Player.p_bullets2[j].b_Free=true;
                //播放爆炸效果
                for(int k=0;k<BOMB_NUM;k++){
                    if(bombs[k].Free){
                        //空闲爆炸
                        bombs[k].Free=false;
                        //更新爆炸坐标
                        bombs[k].bombX=enemys[i].X;
                        bombs[k].bombY=enemys[i].Y;
                        break;
                    }
                }
            }
        }
        //敌机与飞机相撞
        if(Player.p_Rect.intersects(enemys[i].Rect)){
            enemys[i].Free=true;
            if(PlayerBomb.Free){
                Player.Blood--;
                if(Player.Blood==0){
                    PlayerBomb.Free=false;
                    PlayerBomb.bombX=Player.p_X;
                    PlayerBomb.bombY=Player.p_Y;
                }
            }
        }
    }

}


